
<?php 

require_once "./_rsa.inc";

$key = generate_keys(512);

echo "pubkey:<BR>";

echo "public exponent:\t";
echo $key["publicexponent"];
echo "<BR>";
echo "modulus:";
echo $key["modulus"];
echo "<BR><BR>";

echo "privatekey:<BR>";

echo "private exponent:  ";
echo $key["privateexponent"];
echo "<BR>";
echo "prime p:  ";
echo $key["primep"];
echo "<BR>";
echo "prime q:  ";
echo $key["primeq"];
echo "<BR><BR>";

$plaintext = "Anyway, I keep picturing all these little kids playing some game in this big field of rye and all.  Thousands of little kids, and nobody's around - nobody big, I mean - except me.  And I'm standing on the edge of some crazy cliff.  What I have to do, I have to catch everybody if they start to go over the cliff - I mean if they're running and they don't look where they're going I have to come out from somewhere and catch them.  That's all I do all day.  I'd just be the catcher in the rye and all.  I know it's crazy, but that's the only thing I'd really like to be.";

echo "<BR>";
echo "plaintext:  ";
echo $plaintext;
echo "<BR><BR>";

$ciphertext = rsa_encrypt_text($plaintext, $key["publicexponent"], $key["modulus"]);

echo "ciphertext:  ";
echo implode($ciphertext);
echo "<BR><BR>";

$m = rsa_decrypt_text($ciphertext, $key["privateexponent"], $key["modulus"]);

echo "decrypted ciphertext:  ";
echo $m;
echo "<BR><BR>";


echo "MD5 hash of above text:";
echo md5($plaintext);
echo "<BR><BR>";

$s = rsa_sign($plaintext, $key["privateexponent"], $key["modulus"]);

echo "Signature of above text:  ";
echo $s;
echo "<BR>";

$sv = rsa_check_signature($s, $key["publicexponent"], $key["modulus"]);

echo "Signature Validation:  ";
echo $sv;
echo "<BR><BR>";

if(md5($plaintext) == $sv)
	echo "The signature is valid.";
else 
	echo "The signature is not valid.";

?>
